package com.guashu.jaywayjsonpath.test;

import com.guashu.jaywayjsonpath.util.DealMessageUtil;
import com.jayway.jsonpath.*;
import org.junit.Test;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class StringSubstringExample {
    @Test
    public void test1() {
        String inputString = "$.PDA.PD01[*].PD01A.PD01AD01";
        String substring = DealMessageUtil.getSubstringLeft(inputString);
        System.out.println("截取左边的结果：" + substring);
        String substringRight = DealMessageUtil.getSubstringRight(inputString);
        System.out.println("截取右边的结果：" + substringRight);
    }

    @Test
    public void getFirstArray() {
        String input = "$.PDA.PD01[*].PD01E.PD01EH[*].PD01ER03";
        String result = DealMessageUtil.getFirstArray(input);
        System.out.println("二维数组第一个数组： " + result);
    }

    @Test
    public void getMidderArray() {
        String input = "$.PDA.PD01[*].PD01E.PD01EH[*].PD01ER03";
        String extracted = DealMessageUtil.getMidderArray(input);
        System.out.println("Extracted: " + extracted);
    }

    @Test
    public void getLast() {
        String input = "$.PDA.PD01[*].PD01E.PD01EH[*].PD01ER03";
        String result = DealMessageUtil.getLast(input);
        System.out.println("第二个[*]后面的字符为: " + result);
    }

    /**
     * 替换JSON中的节点，将电话号码中间四位替换为*
     */
    @Test
    public void jsonPathReplace() {
        String json = "{" +
                "\"data\":[" +
                "{\"name\":\"John\",\"phone\":\"15013705088\"}," +
                "{\"name\":\"老王\",\"phone\":\"13569695485\"}," +
                "{\"name\":\"Jane\",\"phone\":\"13869596459\"}" +
                "]," +
                "\"user\":{" +
                "\"name\":\"lizz\"" +
                "}," +
                "\"code\":\"123\"" +
                "}";

        System.out.println("Original JSON:");
        System.out.println(json);

        // 解析 JSON
        DocumentContext document = JsonPath.parse(json);

        // 获取要替换的节点的路径
        String path = "$.data[*].phone";

        // 执行节点替换操作
        document.map(path, (currentValue, configuration) -> {
            // 获取电话号码
            String phone = (String) currentValue;
            // 如果电话号码长度大于等于7，则将第4到7位替换为 *
            if (phone != null && phone.length() >= 7) {
                String replacedPhone = phone.substring(0, 3) + "****" + phone.substring(7);
                return replacedPhone;
            }
            // 如果电话号码长度不足7位，直接返回原始值
            return phone;
        });

        // 获取更新后的 JSON
        String updatedJson = document.jsonString();
        System.out.println("Updated JSON:");
        System.out.println(updatedJson);
    }

    /**
     * 替换JSON中的节点，补齐null为非null
     */
    @Test
    public void jsonPath() {
        String json = "{" +
                "\"data\":[" +
                "{\"name\":\"John\",\"phone\":null}," +
                "{\"name\":\"老王\",\"phone\":null}," +
                "{\"name\":\"Jane\",\"phone\":null}" +
                "]," +
                "\"user\":{" +
                "\"name\":\"lizz\"" +
                "}," +
                "\"code\":\"123\"" +
                "}";

        System.out.println("Original JSON:");
        System.out.println(json);

        // 解析 JSON
        DocumentContext document = JsonPath.parse(json);

        // 获取要替换的节点的路径
        String path = "$.data[*].phone";

        // 定义一个后缀计数器
        AtomicInteger counter = new AtomicInteger(1);

        // 执行节点替换操作
        document.map(path, (currentValue, configuration) -> {
            // 数据处理，返回替换后的内容
            String phone = (String) currentValue;
            String s =null;
            if (phone == null) {
                // 添加前缀和后缀
                 s = "abc_" + counter.getAndIncrement() + "_" + phone;

            }
            return s;
        });

        // 获取更新后的 JSON
        String updatedJson = document.jsonString();
        System.out.println(updatedJson);
    }

    @Test
    public void testContain() {
        String inputString = "$.PDA.PD01[*].PD01E.PD01EH[*].PD01ER03";

//        // 判断字符串中是否包含一个[*]
//        boolean containsOne = inputString.indexOf("[*]") != -1;
//
//        // 判断字符串中是否包含两个[*]
//        int firstIndex = inputString.indexOf("[*]");
//        int secondIndex = inputString.indexOf("[*]", firstIndex + 1);
//        boolean containsTwo = secondIndex != -1;



        // 找到第一个[*]的位置
        int index = inputString.indexOf("[*]");

        // 截取第一个[*]左边的字符
        String leftSubstring = inputString.substring(0, index)+"[*]";

        System.out.println("firstSplitElement: " + leftSubstring);
//        String firstSplitElementRight= inputString.split("[*]")[1];
//        if(!StringUtils.isEmpty(firstSplitElementRight) && firstSplitElementRight.contains("[*]")){
//            String secondSplitElement= "$"+inputString.split("[*]")[0];
//            System.out.println("secondSplitElement: " + secondSplitElement);
//        }


    }

    @Test
    public void testInsertSql() {
        List<String> insertTmp = new ArrayList<>();
        List<String> colunm = new ArrayList<>();
        insertTmp.add("'张三','21','男',");
        insertTmp.add("'李四','18','男',");
        insertTmp.add("'王熙凤','31','女',");
        insertTmp.add("'关羽','45','男',");

        colunm.add("user_name");
        colunm.add("age");
        colunm.add("xingbie");

    }
}
